﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

using HtmlAgilityPack;
using Interfaces;

namespace MyWebpageFilter
{
    public partial class MetaWebpageFilterPlugin : UserControl, IWebpageFilter
    {
        public MetaWebpageFilterPlugin()
        {
            InitializeComponent();
        }

        private IPreparationHost pluginHost = null;
        public IPreparationHost PluginHost 
        {
            get
            {
                return pluginHost;
            }
            set
            {
                this.pluginHost = value;
            }
        }

        private string pluginName = "Meta Tag Webpage Filter";
        public string PluginName 
        {
            get
            {
                return this.pluginName;
            }
        }

        private string pluginDescription = "Filter Webpages which has no meta tag.";

        public string PluginDescription
        {
            get { return pluginDescription; }
            set { pluginDescription = value; }
        }


        public UserControl PluginUserControl 
        {
            get
            {
                return this;
            }
        }

        public void Initialize()
        {
        }

        public void PluginDispose()
        {
        }

        public bool IsDesirableWebpage(string htmlStr)
        {
            bool result = false; // The result to be returned

            try
            {
                // Parse html string to html document
                HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();
                htmlDoc.LoadHtml(htmlStr);

                // Check if it is desirable: Having meta keywords
                foreach (HtmlNode curNode in htmlDoc.DocumentNode.Descendants("meta"))
                {
                    if ((curNode.Attributes["name"] != null) && (curNode.Attributes["name"].Value == "keywords"))
                    {
                        result = true;
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return result;
        }
    }
}
